package org.apache.zookeeper.client;

import org.apache.zookeeper.*;

import java.io.IOException;

/**
 * <h2>原生客户端</h2>
 * description:
 *
 * @author yongjun.ji(yongjun.ji @ ucarinc.com)
 * @since 1.0 2020/6/15 11:14
 */
public class ZookeeperClientTest {
    public static void main(String[] args) throws IOException {
        ZooKeeper client = new ZooKeeper("localhost:2181", 60 * 1000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("默认Watcher：" + event);
            }
        });

        String path = "/testZookeeperClient";
        try {
            String result = client.create(path, "测试zookeeper客户端".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
            System.out.println(result);
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            byte[] data = client.getData(path, event -> System.out.println("getData Watcher:" + event), null);
            System.out.println(new String(data));
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.in.read();
    }
}
